﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Application.AppConfigureManage;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;
using Zocono.WCS.Infrastructure.ApllicationDto;

namespace Zocono.WCS.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RouteInfoController : ControllerBase
    {
        private readonly ILogUtil _LogUtil;
        private readonly RouteInfoApp _RouteInfoApp;

        public RouteInfoController(ILogUtil logUtil, RouteInfoApp routeInfoApp)
        {
            _LogUtil = logUtil;
            _RouteInfoApp = routeInfoApp;
        }

        [HttpPost]
        public async Task<HeaderResult<List<RouteInfo>>> GetPagedRouteInfoAsync(QueryRouteInfoDto queryRouteInto)
        {
            HeaderResult<List<RouteInfo>> headerResult = new HeaderResult<List<RouteInfo>>();
            try
            {
                PageDto<RouteInfo> pageDto = await _RouteInfoApp.GetPagedRouteInfoAsync(queryRouteInto);
                headerResult.IsSucceed = true;
                headerResult.Result = pageDto.Result;
                headerResult.Total = pageDto.Total;
                headerResult.Message = "查询成功";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpGet]
        public async Task<HeaderResult<List<RouteInfo>>> GetRouteInfoListAsync()
        {
            HeaderResult<List<RouteInfo>> headerResult = new HeaderResult<List<RouteInfo>>();
            try
            {
                var list = await _RouteInfoApp.GetRouteInfoListAsync();
                headerResult.IsSucceed = true;
                headerResult.Result = list;
                headerResult.Total = list.Count;
                headerResult.Message = "查询成功";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpPost]
        public async Task<HeaderResult<bool>> CreateRouteInfoAsync(RouteInfo routeInfo)
        {
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            try
            {
                var result = await _RouteInfoApp.CreateRouteInfoAsync(routeInfo);
                headerResult.IsSucceed = result;
                headerResult.Result = result;
                headerResult.Message = result ? "新增成功" : "新增失败";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpPost]
        public async Task<HeaderResult<bool>> ModifyRouteInfoAsync(RouteInfo routeInfo)
        {
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            try
            {
                var result = await _RouteInfoApp.ModifyRouteInfoAsync(routeInfo);
                headerResult.IsSucceed = result;
                headerResult.Result = result;
                headerResult.Message = result ? "修改成功" : "修改失败";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpPost]
        public async Task<HeaderResult<bool>> DeleteRouteInfoAsync(List<RouteInfo> routeInfoList)
        {
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            try
            {
                var result = await _RouteInfoApp.DeleteRouteInfoAsync(routeInfoList);
                headerResult.IsSucceed = result;
                headerResult.Result = result;
                headerResult.Message = result ? "删除成功" : "删除失败";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }
    }
}
